package com.his.util.util;

import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Tools {

    public static Random random = new Random();

    public static Long getDate() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateStr = format.format(date);

        return Long.parseLong(dateStr);
    }

    public static Long getDate(String date) {
        String[] strings = date.split("-");

        String temp = "";

        for (String str : strings) {
            temp += str;
        }

        return Long.parseLong(temp);
    }

    public static String generateSalt() {
        String str = "";

        for (int i = 0; i < 8; i++) {
            str += (char) random.nextInt(128);
        }

        str = string2MD5(str);
        //直接返回加盐
        return str;
    }

    public static String encryption(String plaintexts, String salt) {
        String ciphertexts = convertMD5(plaintexts + salt);

        return ciphertexts;
    }

    public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    public static String convertMD5(String inStr){
        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++){
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;
    }

    public static String generateSuffix() {
        String str = "";

        for (int i = 0; i < 32; i++) {
            switch (random.nextInt(3)) {
                case 0: str += random.nextInt(25) + 'A'; break;
                case 1: str += random.nextInt(25) + 'a'; break;
                case 2: str += random.nextInt(10) + '0'; break;
            }
        }

        return str;
    }

    public static String readFile(String filePath) {
        String context = "";

        try {
            URL url =new URL(filePath);
            URLConnection urlconn = url.openConnection();

            InputStreamReader inputStreamReader = new InputStreamReader(urlconn.getInputStream(), "UTF-8");

            int temp;

            while ((temp = inputStreamReader.read()) != -1) {
                context += (char) temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return filePath;
        }


        return context;
    }

    public static Integer pageNumber(Integer num, Integer num1) {
        return num / num1 + (num % num1 == 0 ? 0 : 1);
    }
}